<?php

namespace app\controllers\h5\services;

use app\redis\Cache;
use app\services\base\BaseService;
use app\models\product\ProductCategory as Model;

/**
 * provide
 *  uni/category
 *  category
 *
 * Class Category
 * @package app\controllers\h5\services
 */
class Category
{
    use BaseService;

    /**
     * get children category list ( include 2 & 3 level children)
     *
     * @param $parent_id
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public static function getChildrenByParent($parent_id)
    {
        $key = Cache::$CATEGORIES_BY . $parent_id;
        $cache = Cache::get($key);
        if (!$cache) {
            $list = Model::find()
                ->where(['parent_id' => $parent_id, 'status' => Model::$statusMapping['OK']])
                ->with(['children', 'image', 'children.image'])
                ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
                ->asArray()
                ->all();
            Cache::set($key, json_encode($list));
        } else {
            $list = json_decode($cache, true);
        }

        return $list;
    }


}